<!-- 选择已有公司 -->
<template>
  <div class="page-select-existing-enterprise page-next">
    <Row type="flex" justify="center">
      <Col span="18">
        <CheckCompany
          :parentCompanyId.sync="formData.joinCompanyId"
          @parentCompanyObj="parentCompanyObj"
        />
      </Col>
      <Col span="18" v-if="formData.joinCompanyId">
        <div class="select-company-check">
          <div class="select-company-item">
            <span>{{ parentCompany.companyName }}</span
            ><Checkbox disabled v-model="joinCompany"> </Checkbox>
          </div>
        </div>
      </Col>
      <Col span="16">
        <div class="next-btn">
          <Button
            type="primary"
            :loading="isLoading"
            :disabled="isBtnDisabled"
            @click="handleAgree"
            >确认加入该企业并完成注册</Button
          >
        </div>
      </Col>
    </Row>
  </div>
</template>

<script type="text/javascript">
import CheckCompany from "@/pages/account/register/components/checkCompany.vue";

import { UserAndCompanyRegister } from "@/api/account.js";
import { encrypt } from "@/utils/encrypFile";

export default {
  components: {
    CheckCompany,
  },
  data() {
    return {
      single: "",
      isBtnDisabled: true,
      companyList: [
        {
          id: 1,
          name: "企业名称1",
          isChecked: false,
        },
      ],
      formData: {
        isRegisterNewCompany: false, // 是否注册公司
        joinCompanyId: "",
      },
      parentCompany: {},
      joinCompany: true,
      isLoading: false,
    };
  },
  methods: {
    // handleAgree(){
    //     this.$emit("handleSuccess");
    // }
    parentCompanyObj(item) {
      this.parentCompany = item;
      this.isBtnDisabled = false;
    },
    async handleAgree() {
      let userBase = JSON.parse(localStorage.getItem("registerBaseData"));
      userBase.pwd = encrypt(userBase.pwd);
      userBase.confirmPwd = encrypt(userBase.confirmPwd);
      let params = {
        ...userBase,
        ...this.formData,
      };
      this.isLoading = true;
      try {
        let data = await UserAndCompanyRegister(params);
        this.isLoading = false;
        this.$emit("handleSuccess", this.formData);
      } catch (error) {
        this.isLoading = false;
      }
    },
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less" scoped>
.page-select-existing-enterprise {
  margin-top: 20px;
}
.select-company-item {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  margin-top: 20px;
  border: 1px solid #c4c4c4;
  border-radius: 4px;
}
.next-btn {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}
.ivu-btn {
  width: 100%;
}
</style>
